Break, Continue and Goto in C++
Break statement in C++ is like an emergency exit button for loops. When you press it, you can get out of a loop before it's supposed to end. It's like saying, "I'm done with this loop now!" and your program continues with whatever comes after the loop.
1. Usage in Loops: The break statement is typically used within loops (such as for, while, or do-while loops) to prematurely exit the loop.
2. Conditional: It is usually used in conjunction with an if statement to specify a condition under which the loop should be exited.
3. Immediate Exit: When a break statement is encountered, it immediately exits the innermost loop it is contained within.
4. Switch Statements: In addition to loops, break can also be used in switch statements to exit the switch block after a specific case is matched.
5. Neting Loopss: If you have nested loops (loops inside other loops), a break statement will only exit the innermost loop, not the outer ones. If you want to exit multiple loops simultaneously, you might need additional logic or labels.
6. No Value: The break statement doesn't return any value; it's simply used for control flow.
7. Semicolon: Be sure to terminate the break statement with a semicolon, like any other C statement.
Example of Break statement:
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
if (i == 3) {
// Exit the loop when i equals 3
break;
}
}
return 0;
}
Continue statement in C++ acts like a "skip" button for loops. When it's used, it tells the program to jump over the current step of the loop and go directly to the next one. So, if you encounter a continue statement, you're saying, "I don't want to finish this part of the loop; let's move on to the next iteration right away."
The continue statement in C++ is like a "skip ahead" command used within loops. When it's encountered, it acts as a signal to jump back to the beginning of the loop for the next iteration, completely avoiding any code that comes after it within the loop's body.
In simpler terms, it's like saying, "I want to move on to the next round of the loop right now." You can use it when you want to skip over specific parts of the loop based on certain conditions.
Unlike the break statement, which can exit the entire loop, continue keeps you inside the loop and ensures that the next round of the loop starts right away.
Example of Continue statement:-
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
// Skip the loop iteration when i is equal to 3
if (i == 3) {
continue;
}
// Output the current value of i
cout << i << " ";
}
cout << endl;
return 0;
}
Output:-
goto statement is used to transfer control within a program to a labeled statement. It allows you to create unconditional jumps within your code. The goto statement is not commonly used in modern programming practices, as it can make code less structured and harder to understand. It can also lead to spaghetti code if used excessively. Instead, you should typically use structured control flow constructs like if, while, for, and functions to control program flow. However, the goto statement can still be used in some specific situations.
int main() {
int i = 0;
start:
if (i < 5) {
printf(""i = %d\n", i);
i++;
goto start; // Jump back to the "start" label
}
return 0;
}
Output:-
1.The program uses a goto statement to create a label named start.
2. Inside the loop, it prints the value of i and increments it.
3. After printing the value, it checks if i is less than or equal to 5. If true, it jumps to the start label using goto.
4. The loop continues until i becomes greater than 5.
5. After the loop, a newline is printed for better formatting.